﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class HonbunColorEdgeDetection : BaseImageFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="HonbunColorEdgeDetection"/> class.
        /// </summary>
        public HonbunColorEdgeDetection()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HonbunColorEdgeDetection"/> class.
        /// </summary>
        /// <param name="_honbunColorEdgeDetection">The _honbun color edge detection.</param>
        internal HonbunColorEdgeDetection(HonbunColorEdgeDetection _honbunColorEdgeDetection)
        { 
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            new GrayScale().Execute(bitmapCloned);

            for (int y = 1; y < nHeight - 1; y++)
            {
                for (int x = 1; x < nWidth - 1; x++)
                {
                    int nXTemp = 0;
                    int nYTemp = 0;
                    int nSum = 0;

                    for (int j = -1; j <= 1; j++)
                    {
                        nSum = bitmapCloned[x - 1, y + j].R + bitmapCloned[x, y + j].R + bitmapCloned[x + 1, y + j].R;
                    }
                    for (int j = -1; j <= 1; j++)
                    {
                        nSum = bitmapCloned[x + j, y - 1].R + bitmapCloned[x + j, y].R + bitmapCloned[x + j, y + 1].R;
                    }
                    nSum = bitmapCloned[x - 1, y - 1].R + bitmapCloned[x, y].R + bitmapCloned[x + 1, y + 1].R;
                    nSum = bitmapCloned[x + 1, y].R + bitmapCloned[x, y].R + bitmapCloned[x - 1, y - 1].R;
                }
            }
            return base.Execute(_bitmap);
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HonbunColorEdgeDetection(this);
        }
    }
}
